/**
 * 
 */
package com.audi.message;

import lib.relay.ThreadCtrlBase;
import android.os.Handler;
import android.util.Log;

import com.audi.dto.DeviceItem;
import com.audi.dto.MemberItem;
import com.audi.dto.PlaceItem;
import com.audi.dto.SceneItem;
import com.google.gson.Gson;

/**
 * @author PhongNT11
 * 
 */
public class SendMessage {

	public Connector connector;

	/**
	 * Class constructor specifying Context to create
	 * 
	 * @see create Connector
	 */
	public SendMessage() {
		connector = new Connector();
	}

	/**
	 * addDevice
	 * 
	 * @param item
	 *            DeviceItem
	 * @param handler
	 *            Handler
	 * @see add new device
	 */
	public void addDevice(DeviceItem item, Handler handler) {
		Gson g = new Gson();
		String data = g.toJson(item);

		AudiMessage msg = new AudiMessage();
		msg.setActionID(ActionMessage.addDevice);
		msg.setData(data);

		String msgJson = g.toJson(msg);

		connector.sendMessage(msgJson, handler);
	}
	
	public void sendData(int actionId, Object obj, Handler handler)
	{
		Gson g = new Gson();
		String data = g.toJson(obj);

		AudiMessage msg = new AudiMessage(actionId, data);

		String msgJson = g.toJson(msg);
		Log.d("Client", "[SendMessage.java] sendData " + msgJson);
		connector.sendMessage(msgJson, handler);
	}
	
	public void sendData(int actionId, Object obj1, Object obj2, Handler handler)
	{
		Gson gson = new Gson();
		String strObj1 = gson.toJson(obj1);
		String strObj2 = gson.toJson(obj2);
		ObjectCombine objCombine = new ObjectCombine(strObj1, strObj2);
		String data = gson.toJson(objCombine);	
		Log.d("sendData2:", data);	
		
		AudiMessage msg = new AudiMessage(actionId, data);
		String msgJson = gson.toJson(msg);
		Log.d("Client", "[SendMessage.java] sendData2 " + msgJson);
		connector.sendMessage(msgJson, handler);
	}

	/**
	 * addMember
	 * 
	 * @param item
	 *            MemberItem
	 * @param handler
	 *            Handler
	 * @see add new Member
	 */
	public void addMember(MemberItem item, Handler handler) {
		Gson g = new Gson();
		String data = g.toJson(item);
		AudiMessage msg = new AudiMessage();
		msg.setActionID(ActionMessage.addMember);
		msg.setData(data);
		String msgJson = g.toJson(msg);
		connector.sendMessage(msgJson, handler);
	}

	/**
	 * getListDevice
	 * 
	 * @param handler
	 *            Handler
	 * @see get list device
	 */
	public void getListDevice(Handler handler) {
		Gson g = new Gson();
		AudiMessage msg = new AudiMessage();
		msg.setActionID(ActionMessage.getAllDeviceList);
		msg.setData("");
		String msgJson = g.toJson(msg);
		connector.sendMessage(msgJson, handler);
	}
	/**
	 * Get list member
	 * @param handlerMember
	 */
	public void getListMember(Handler handlerMember)
	{
		Gson gMember = new Gson();
		AudiMessage msgMember = new AudiMessage();
		msgMember.setActionID(ActionMessage.getListMember);
		msgMember.setData("");
		String msgJsonMeber = gMember.toJson(msgMember);
		connector.sendMessage(msgJsonMeber, handlerMember);
	}
	/**
	 * getListPlace
	 * 
	 * @param handler
	 *            Handler
	 * @see get list place
	 */
	public void getListPlace(Handler handler) {
		Gson g = new Gson();
		AudiMessage msg = new AudiMessage();
		msg.setActionID(ActionMessage.getAllPlaceList);
		msg.setData("");
		String msgJson = g.toJson(msg);
		connector.sendMessage(msgJson, handler);
	}

	/**
	 * addPlace
	 * 
	 * @param item
	 *            PlaceItem
	 * @param handler
	 *            Handler
	 * 
	 * @see add new place
	 */
	public void addPlace(PlaceItem item, Handler handler) {
		Gson g = new Gson();
		String data = g.toJson(item);
		AudiMessage msg = new AudiMessage();
		msg.setActionID(ActionMessage.addPlace);
		msg.setData(data);
		String msgJson = g.toJson(msg);
		connector.sendMessage(msgJson, handler);
	}
	public void updateDevice(DeviceItem item ,Handler handler) {
		Gson g = new Gson();
		String data = g.toJson(item);
		AudiMessage msg = new AudiMessage();
		msg.setActionID(ActionMessage.UPDATE_DEVICE);
		msg.setData(data);
		String msgJson = g.toJson(msg);
		connector.sendMessage(msgJson, handler);
	}
	/**
	 * renamePlace
	 * 
	 * @param item
	 *            PlaceItem
	 * @param handler
	 *            Handler
	 * 
	 * @see rename place
	 */
	public void renamePlace(PlaceItem item, Handler handler) {
		Gson g = new Gson();
		String data = g.toJson(item);
		AudiMessage msg = new AudiMessage();
		msg.setActionID(ActionMessage.renamePlace);
		msg.setData(data);
		String msgJson = g.toJson(msg);
		connector.sendMessage(msgJson, handler);
	}

	/**
	 * changeCapabilityPlace
	 * 
	 * @param item
	 *            PlaceItem
	 * @param handler
	 *            Handler
	 * 
	 * @see change Capability of Place
	 */
	public void changeCapabilityPlace(PlaceItem item, Handler handler) {
		Gson g = new Gson();
		String data = g.toJson(item);
		AudiMessage msg = new AudiMessage();
		msg.setActionID(ActionMessage.changeCapabilityPlace);
		msg.setData(data);
		String msgJson = g.toJson(msg);
		connector.sendMessage(msgJson, handler);
	}

	/**
	 * deletePlace
	 * 
	 * @param item
	 *            PlaceItem
	 * @param handler
	 *            Handler
	 * 
	 * @see delete Place
	 */
	public void deletePlace(PlaceItem item, Handler handler) {
		Gson g = new Gson();
		String data = g.toJson(item);
		AudiMessage msg = new AudiMessage();
		msg.setActionID(ActionMessage.deletePlace);
		msg.setData(data);
		String msgJson = g.toJson(msg);
		connector.sendMessage(msgJson, handler);
	}
	/** HoangNT110
	 *  Method updata data for scene
	 * @param item
	 * @param handler
	 */
	public void updateScene(SceneItem item ,Handler handler) {
		Gson g = new Gson();
		String data = g.toJson(item);
		AudiMessage msg = new AudiMessage();
		msg.setActionID(ActionMessage.UPDATE_SCENE);
		msg.setData(data);
		String msgJson = g.toJson(msg);
		connector.sendMessage(msgJson, handler);
	}
	/** HoangNT10
	 * Method delete data for scene
	 * @param item
	 * @param handler
	 */
	public void deleteScene(SceneItem item ,Handler handler) {
		Gson g = new Gson();
		String data = g.toJson(item);
		AudiMessage msg = new AudiMessage();
		msg.setActionID(ActionMessage.DELETE_SCENE);
		msg.setData(data);
		String msgJson = g.toJson(msg);
		connector.sendMessage(msgJson, handler);
	}
	public void checkExistScene(SceneItem item ,Handler handler) {
		Gson g = new Gson();
		String data = g.toJson(item);
		AudiMessage msg = new AudiMessage();
		msg.setActionID(ActionMessage.CHECK_SCENE_EXIST);
		msg.setData(data);
		String msgJson = g.toJson(msg);
		connector.sendMessage(msgJson, handler);
	}
}
